S = AlphabeticStrings()
temp = open("Ciphertext/cipher05.txt")
ct05 = S(temp.read())
temp.close()
n = len(ct05)      
for m in range(1,10):
          cis = [ ct05[i:n:m].coincidence_index() for i in range(m) ]
          print "%s : %s" % (m, sum(cis)/m)
1 0.04128012438455558434827675563
2 0.04252125629012667013708138122
3 0.04116798565358075708549374293
4 0.04333449356073790462930734423
5 0.04067538126361655773420479302
6 0.04311525495153813737884534344
7 0.05476804123711340206185567010
8 0.04296218487394957983193277310
9 0.04264264264264264264264264264
for m in range(10,19):
          cis = [ ct05[i:n:m].coincidence_index() for i in range(m) ]
          print "%s : %s" % (m, sum(cis)/m)
10 0.04205443371378402107111501316
11 0.04187779433681073025335320417
12 0.04415584415584415584415584415
13 0.03927369764473836872026917275
14 0.07231509625126646403242147922
15 0.03939393939393939393939393938
16 0.04478803716608594657375145179
17 0.04185520361990950226244343891
18 0.04479479479479479479479479479
def translation_correlations(X1,X2):
     """
     The sequence of correlations of the sequence X1 with the cyclic 
     translations of the sequence X2.
     """
     n = len(X1)
     if n != len(X2):
         raise ValueError, "Arguments must be of the same length." 
 
     # Compute the mean value of each sequence: 
     mu1 = sum(X1)/n
     mu2 = sum(X2)/n
 
     # Compute the standard deviations of each sequence:
     sig1 = sqrt(sum([ (S1[k]-mu1)^2 for k in range(n) ]))
     sig2 = sqrt(sum([ (S2[k]-mu2)^2 for k in range(n) ]))
 
     sig = sig1*sig2
     corr_dict = { }
     for j in range(n):
         corr_dict[j] = sum([ 
             (S1[i] - mu1) * (S2[(i+j)%n] - mu2) / sig for i in range(n) ])
     return corr_dict
def translation_matches(S,X,r) 
    """
    Input
       S : Test string.
       X : Sequence of standard frequencies for the language.
       r : A real number between 0 and 1.
    Output
       A list of integers k such that affine translation of S by k 
       has correlation at least r with the standard frequencies 
       given by the real sequence X.
    """
    Y = S.frequency_distribution()
    corr_dict = translation_correlations(X,Y)
    I = []
    for i in keys(X):
        if x[2] > r:
            I.append(x[1])
    return I
PT = open("Plaintext/blackcat.txt").read()
CT := open("Ciphertext/cipher01.txt").read()
X = frequency_distribution(PT)
for i in range(11):
          translation_matches(CT[i::11],X,0.50)
[ 8 ]
[ 19 ]
[ 26 ]
[ 16 ]
[ 22 ]
[ 8 ]
[ 11 ]
[ 22 ]
[ 26 ]
[ 9 ]
[ 22 ]

def relative_differential_probabilities(D1,D2,PT):
    """
    Given 2-character strings D1 and D2 representing XY-ZW and XY-QR, 
    return the dictionary of probabilities (P(XY)P(ZW)P(QR)), scaled 
    to a probability function on the set {A,..,Z}^2.
    PT is a sample plaintext for use in determining the 2-character 
    frequency distribution of English.
    """
    if len(D1) != 2 or len(D2) != 2:                                             
        raise ValueError, \
           "Arguments D1 (= %s) and D2 (= %s) must have length 2" % (D1, D2)
    AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    r1 = AZ.index(D1[0]); s1 = AZ.index(D1[1])
    r2 = AZ.index(D2[0]); s2 = AZ.index(D2[1])
    FD = RealField()(0)                                                     
    D2 = {}
    F2 = frequency_distribution(PT,2)                                  
    for i1 in range(26):
       X1 = AZ[i1]; X2 = AZ[(i1-r1)%26]; X3 = AZ[(i1-r2)%26]
       for j1 in range(26):
            Y1 = AZ[j1]; Y2 = AZ[(j1-s1)%26]; Y3 = AZ[(j1-s2)%26]
            D2[X1+Y1] = F2[X1+Y1] * F2D[X2+Y2] * F2D[X3+Y3]
            FD += D2[X1+Y1]
    c = 1/FD
    for i1 in range(26):
        X1 = AZ[i1]
        for j1 in range(26):
            Y1 = AZ[j1]
            D2[X1+Y1] *= c
    return D2
AZ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
PT = strip_encoding(open("Plaintext/blackcat.txt").read())
Diff1 = "AHXCOYFBAMKUE"
Diff2 = "XHXRGEUHPRAHN"
eps = 0.15
for k in range(12):
    D1 = Diff1[[k,k+1]]; D2 = Diff2[[k,k+1]];
    FD = relative_differential_probabilities(D1,D2,PT)
    r1 = AZ.index(D1[0]); s1 = AZ.index(D1[1])
    r2 = AZ.index(D2[0]); s2 = AZ.index(D2[2])
    print "k: %s" % k
    for i1 in range(26):
        for j1 in range(26):
            AB = AZ[i1] + AZ[j1]
            if FD.has_key(AB) and FD[AB] > eps:
                i2 = (i1-r1) % 26; j2 = (j1-s1) % 26
                i3 = (i1-r2) % 26; j3 = (j1-s2) % 26
                print "  [%s,%s,%s]: %s" % (
                    AZ[[i1,j1]], AZ[[i2,j2]], AZ[[i3,j3]], FD[i1,j1])
k: 1
  ['TO','TH','WH']: 0.2716182630
k: 2
k: 3
  ['BE','EC','EN']: 0.15547486679
k: 4
k: 5
  ['OR','AT','IN']: 0.5823337200
k: 6
  ['LY','NT','HE']: 0.4247418881
k: 7
  ['NO','IN','TH']: 0.6867756526
k: 8
  ['OT','NT','HE']: 0.24752628407
k: 9
  ['TT','TH','EC']: 0.2612827757
k: 10
  ['ED','ST','ND']: 0.2883526922
  ['TO','HE','CO']: 0.19345495180
k: 11
  ['OM','ES','OF']: 0.17007055156
  ['OU','EA','ON']: 0.2627616522
k: 12
p = 1172991670841347272989353064539
q = 300997517969507552061104346547
e = 5
d1 = inverse_mod(e,p-1)
d1
703795002504808363793611838723
d2 = inverse_mod(e,q-1)
d2
240798014375606041648883477237
gcd(p-1,q-1);
6
(p-1).factor()
2 * 3^3 * 13 * 23767 * 19475307419 * 3609932889503
(q-1).factor()
2 * 3 * 17 * 5297 * 22123 * 152417 * 165217231734649
d1 mod 6
5
d2 mod 6
5
d = crt([d1,d2],[p-1,(q-1).div(6)])
d
35306758152215111348997570443072341096420788599987705538575
d = inverse_mod(e,lcm(p-1,q-1))
d
35306758152215111348997570443072341096420788599987705538575
(e*d).mod(p-1)
1
(e*d).mod(q-1)
1
n = p*q
m = random_prime(n)
c = m.powermod(e,n)
m == c.powermod(d,n)
True
m == m.powermod(e*d,n)
True
E := RSACryptosystem(128)
m = E.encoding('The dog ate my lunch.'); m
0101010001101000011001010010000001100100011011110110011100100\
0000110000101110100011001010010000001101101011110010010000001\
1011000111010101101110011000110110100000101110
E.decoding(m)
'The dog ate my lunch.'
(K, L) = E.random_key_pair()
K
(49338921862830381807760291818994034053, 86398677368792768067556452456311743331)
x = K[0]; x
86398677368792768067556452456311743331
x.factor()
6046864213681032211 * 14288178850339607921
e = 49338921862830381807760291818994034053
p = 6046864213681032211
q = 14288178850339607921
d = inverse_mod(e,lcm(p-1,q-1))
d
285484457605725559400259141876035917
p = 2^32+15
m = p-1
(m1, m2, m3, m4, m5) = (m.div(q) for q in (2,3,5,131,364289))
powermod(3,m1,p);
4294967310
powermod(3,m2,p);
2086193154
powermod(3,m3,p);
239247313
powermod(3,m4,p);
1859000016
powermod(3,m5,p);
1338913740
FF = FiniteField(p)
x = FF(3)
for i in range(4):
          y = FF.random_element()
          time n = log(x,y)
p = 2^32+61
m = (p-1).quo_rem(2)[0]
2.powermod(m,p)
4294967356
m = (p-1).quo_rem(1073741839)[0]
2.powermod(m,p)
16
FF = FiniteField(p)
x = FF(2)
for i in range(4):
           y = FF.random_element()
           time n = log(x,y)
816373
986893
931102
62625
p = 2^131+1883
fac = (p-1).factor(); fac
2 * 3 * 5 * 37 * 634466267339108669 * 3865430919824322067
primes = [ f[0] for f in (p-1).factor() ]
(p1, p2, p3, p4, p5, p6) = primes
expons = ( (p-1) // r for r in primes )
(n1, n2, n3, n4, n5, n6) = expons
FF = FiniteField(p)
a = FF(109)
c = FF(1014452131230551128319928312434869768346)
[ a^(n1*i) for i in range(2) ].index(c^n1)
1
[ a^(n2*i) for i in range(3) ].index(c^n2)
2
[ a^(n3*i) for i in range(5) ].index(c^n3)
4
[ a^(n4*i) for i in range(37) ].index(c^n4)
29
crt([1,2,4,29],[2,3,5,37])
29
a5 := a^n5
c5 := c^n5
a5^129
1106532280219457618983939634726858708298
c5
1106532280219457618983939634726858708298
a6 = a^n6
c6 = c^n6
a6^127
809579285918008980133272648385832028198
c6
809579285918008980133272648385832028198
x = CRT([29,129,127],[2*3*5*37,p5,p6]);
x
1075217203476555175652504438224037579
a^x eq c
True
    
LFSR = LFSRCryptosystem()
PT = LFSR.encoding('The dog ate my assignment.'); PT
010101000110100001100101001000000110010001101111011001110
010000001100001011101000110010100100000011011010111100100
100000011000010111001101110011011010010110011101101110011
0110101100101011011100111010000101110
LFSR.decoding(PT)
'The dog ate my assignment.'
PT.binary_decoding()
'The dog ate my assignment.'
